{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:15:31.865074300Z",
     "start_time": "2024-01-21T10:15:31.823025200Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Worksheet \"Sheet\">\n"
     ]
    }
   ],
   "source": [
    "from openpyxl import Workbook\n",
    "# 创建一个工作簿\n",
    "wb = Workbook()\n",
    "# 获取当前活跃的工作表\n",
    "sheet = wb.active\n",
    "print(sheet)\n",
    "# 修改工作表名称\n",
    "sheet.title = '示例工作表'\n",
    "# 保存工作簿\n",
    "wb.save('示例.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [
    "from openpyxl import load_workbook\n",
    "# 加载已存在的Excel文件\n",
    "wb = load_workbook('示例.xlsx')\n",
    "# 获取工作表\n",
    "sheet = wb['示例工作表']"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:15:36.983186200Z",
     "start_time": "2024-01-21T10:15:36.955583800Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [
    "# 在工作表中写入数据\n",
    "sheet['A1'] = '姓名'\n",
    "sheet['B1'] = '年龄'\n",
    "# 写入多行数据\n",
    "data = [\n",
    "    ['张三', 25],\n",
    "    ['李四', 30],\n",
    "    ['王五', 28]\n",
    "]\n",
    "for row in data:\n",
    "    sheet.append(row)\n",
    "# 保存工作簿\n",
    "wb.save('示例.xlsx')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:15:41.645889100Z",
     "start_time": "2024-01-21T10:15:41.615907700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('张三', 25)\n",
      "('李四', 30)\n",
      "('王五', 28)\n"
     ]
    }
   ],
   "source": [
    "from openpyxl import load_workbook\n",
    "# 加载已存在的Excel文件\n",
    "wb = load_workbook('示例.xlsx')\n",
    "# 获取工作表\n",
    "sheet = wb['示例工作表']\n",
    "# 读取工作表中的数据\n",
    "for row in sheet.iter_rows(min_row=2, values_only=True):\n",
    "    print(row)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:15:47.078509300Z",
     "start_time": "2024-01-21T10:15:47.052670500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "# 修改指定单元格的数据\n",
    "sheet['A2'] = '赵六'\n",
    "# 保存工作簿\n",
    "wb.save('示例.xlsx')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:15:51.210351600Z",
     "start_time": "2024-01-21T10:15:51.196496200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('赵六', 25)\n",
      "('李四', 30)\n",
      "('王五', 28)\n"
     ]
    }
   ],
   "source": [
    "# 读取工作表中的数据\n",
    "for row in sheet.iter_rows(min_row=2, values_only=True):\n",
    "    print(row)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:15:55.782128200Z",
     "start_time": "2024-01-21T10:15:55.769131100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "# 在第二行之前插入一行\n",
    "sheet.insert_rows(2)\n",
    "# 在第二列之前插入一列\n",
    "sheet.insert_cols(2)\n",
    "# 保存工作簿\n",
    "wb.save('示例.xlsx')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:12:34.109474900Z",
     "start_time": "2024-01-21T10:12:34.092821Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "# 删除第三行\n",
    "sheet.delete_rows(3)\n",
    "# 删除第三列\n",
    "sheet.delete_cols(3)\n",
    "# 保存工作簿\n",
    "wb.save('示例.xlsx')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:13:47.481965500Z",
     "start_time": "2024-01-21T10:13:47.461608100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "# 在工作表中使用公式\n",
    "sheet['C1'] = '总分'\n",
    "sheet['C2'] = '=SUM(A2:B2)'\n",
    "# 保存工作簿\n",
    "wb.save('示例.xlsx')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:17:22.927293400Z",
     "start_time": "2024-01-21T10:17:22.895475700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "from openpyxl.styles import Font, Alignment\n",
    "# 设置字体样式\n",
    "font = Font(name='宋体', bold=True)\n",
    "# 设置对齐样式\n",
    "alignment = Alignment(horizontal='center', vertical='center')\n",
    "# 应用样式到单元格\n",
    "sheet['A1'].font = font\n",
    "sheet['A1'].alignment = alignment\n",
    "# 保存工作簿\n",
    "wb.save('示例.xlsx')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-21T10:22:25.164330600Z",
     "start_time": "2024-01-21T10:22:25.148362200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
